package com.qwe.graduation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qwe.graduation.entity.User;
import com.qwe.graduation.entity.dto.UserDto;
import com.qwe.graduation.mapper.UserMapper;
import com.qwe.graduation.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qwe.graduation.shiro.utils.JwtUtil;
import com.qwe.graduation.utils.exception.Constants;
import com.qwe.graduation.utils.exception.ServiceException;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 林卓锋
 * @since 2023-01-08
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private static final Log LOG = Log.get();

//    @Override
//    public UserDto login(UserDto userDTO) {
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("name", userDTO.getUsername());
//        queryWrapper.eq("password", userDTO.getPassword());
//        // 处理异常情况
//        try {
//            User one = getOne(queryWrapper);
//            return one != null;
//        } catch (Exception e) {
//            LOG.error(e);
//            return false;
//        }
//    }

    @Override
    public UserDto login(UserDto userDTO) {
        User one = getUserInfo(userDTO);
        if (one != null) {
            BeanUtil.copyProperties(one, userDTO, true);
            String token = JwtUtil.sign(one.getId(), one.getPassword());
            userDTO.setToken(token);
            return userDTO;
        } else {
            throw new ServiceException(Constants.CODE_600, "用户名或密码错误");
        }
    }


    private User getUserInfo(UserDto userDTO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", userDTO.getUsername());
        queryWrapper.eq("password", userDTO.getPassword());
        User one;
        try {
            one = getOne(queryWrapper); // 从数据库查询用户信息
        } catch (Exception e) {
            LOG.error(e);
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
        return one;
    }

    @Override
    public User register(UserDto userDTO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", userDTO.getUsername());
        User one;
        try {
           one = getOne(queryWrapper); // 从数据库查询用户信息
        } catch (Exception e) {
            LOG.error(e);
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
        if (one == null) {
            one = new User();
            BeanUtil.copyProperties(userDTO, one, true);
            //给默认值
            one.setAvatar("http://localhost:9903/graduation/ecmweb/pic/45f5e28c4a5c4adf8e5e41167b38d747u=1467101607,507417832&fm=253&fmt=auto&app=138&f=JPEG.webp");
            one.setName(userDTO.getUsername());
            one.setNickName("普通用户");
            one.setUserPhone("");
            save(one);  // 把 copy完之后的用户对象存储到数据库
        } else {
            throw new ServiceException(Constants.CODE_600, "用户已存在");
        }
        return one;
    }
}
